from django.contrib import admin
from car.models import Car, CarInfo, CarsComment
from user.models import User, Comment, Topic


# Register your models


class UserAdmin(admin.ModelAdmin):
    list_per_page = 20
    list_display = ( "name","username",  "email", "birthday", "webname")
    search_fields = ("username", "name", "email", "webname")
    def webname(self, obj):
        return obj.webname
    webname.short_description = '网名'
    def name(self, obj):
        return obj.name
    name.short_description = '姓名'
    def username(self, obj):
        return obj.username
    username.short_description = '账号'
    def get_user_fields(self, obj=None):
        if obj:
            return ["webname", "name","username"]
        return []

    readonly_fields = property(get_user_fields)
admin.site.register(User, UserAdmin)
##自定义主题
admin.site.site_header = "汽车使用反馈数据分析与可视化系统设计与实现"
admin.site.index_title  = "信息管理"

# Register your models here.
class CarAdmin(admin.ModelAdmin):
    list_per_page = 20
    list_display = ("manufacturers","carname","points","guideprice","hot")
    search_fields =("manufacturers","carname")
    list_filter = ("manufacturers","carname","points")
    list_editable=("guideprice",)
admin.site.register(Car, CarAdmin)

class CarInfoAdmin(admin.ModelAdmin):
    list_display = ('id', 'b_name', 'c_series_name', 'c_spec_name', 'c_level', 'c_engine', 'c_gearbox')  # 在admin界面显示的字段
    list_filter = ('b_name', 'c_series_name', 'c_level')  # 可以用来过滤数据的字段
    search_fields = ('b_name', 'c_series_name', 'c_spec_name')  # 在admin界面添加搜索功能的字段
admin.site.register(CarInfo, CarInfoAdmin)


class CarsCommentAdmin(admin.ModelAdmin):
    list_display =(  'title', 'c_series_name', 'c_spec_name', 'buy_time',"display_user","display_car")
    search_fields = ("title", "c_series_name", "c_spec_name", "address", "satisfied", "dissatisfied","user__username",  "c_series_name","user__name")
    raw_id_fields = ("user","car")  # 使用 raw_id_fields 来搜索外键
    def display_user(self, obj):
        return obj.user.username
    display_user.short_description = '用户'
    def display_car(self, obj):
        return obj.car.carname
    display_car.short_description = '汽车'
    def delete_selected_comments(self, request, queryset):
        # 删除选中的评论
        queryset.delete()
    delete_selected_comments.short_description = "删除选中评论"
admin.site.register(CarsComment, CarsCommentAdmin)

class TopicAdmin(admin.ModelAdmin):
    list_per_page = 20
    list_display = ("title", "display_publisher_name","display_publisher_webnames","publish_time")
    search_fields = ("title", "content", "likes_number","publisher__name","publisher__username")
    def display_publisher_name(self, obj):
        return obj.publisher.name if obj.publisher else "-"  # 如果没有发布者，则返回破折号或其他适当的标识符
    display_publisher_name.short_description = "发布者"
    def display_publisher_webnames(self, obj):
        return obj.publisher.webname if obj.publisher else "-"  # 如果没有发布者，则返回破折号或其他适当的标识符
    display_publisher_webnames.short_description = "网名"

    def display_publisher_username(self, obj):
        return obj.publisher.username if obj.publisher else "-"  # 如果没有发布者，则返回破折号或其他适当的标识符
    display_publisher_username.short_description = "账号"
admin.site.register(Topic, TopicAdmin)

class CommentAdmin(admin.ModelAdmin):
    list_per_page = 20
    list_display = ("display_webname", "display_username", "display_name", "display_topic", "content")
    search_fields = ("topic__title", "user__webname", "content")

    def display_webname(self, obj):
        return obj.user.webname
    display_webname.short_description = '网名'

    def display_username(self, obj):
        return obj.user.username
    display_username.short_description = '用户名'

    def display_name(self, obj):
        return obj.user.name
    display_name.short_description = '姓名'

    def display_topic(self, obj):
        return obj.topic.title
    display_topic.short_description = '标题'


admin.site.register(Comment, CommentAdmin)








